package com.atguigu.springcloud.consumer.controller;


import com.atguigu.common.CommonResult;
import com.atguigu.entity.Payment;

import com.atguigu.springcloud.consumer.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-10-12
 */
@RestController
@RequestMapping("/edu/payment")
public class PaymentController {
    private static final String PAYMENT_URL = "CLOUD-PROVIDER-SERVICE";
    @Autowired
    PaymentService paymentService;
    @Autowired
    RestTemplate restTemplate;
    @GetMapping("selectall")
    public CommonResult<Payment> select(){
        List<Payment> list = paymentService.list();
        return new CommonResult(200,list);
    }
    @GetMapping("selectById/{id}")
    public CommonResult<Payment> selectById(@PathVariable Long id){
        Payment payment = paymentService.getById(id);
        return new CommonResult(200,payment);
    }
    @GetMapping("selectById/{id}")
    public CommonResult<Payment> selectByIdNew(@PathVariable Long id){
//        Payment payment = paymentService.getById(id);

        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
        if(entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();//getForObject()
        }else{
            return new CommonResult<>(444,"操作失败");
        }

    }


}

